package com.jesse.custom.component;

import com.jesse.dao.RadioItem;
import com.jesse.main.R;
import com.jesse.manager.RadioGroupManager;

import android.R.array;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 多功能按钮
 * @author jesse
 */
public class SuperButton extends LinearLayout implements OnTouchListener, RadioItem{

	private final static String TAG = "SuperButton";
	private final static int TEXTBUTTON = 0;
	private final static int IMGZOOMBUTTON = 1;
	private final static int COLORBUTTON = 2;
	private final static int IMGCHANGEBUTTON = 3;
	
	private RelativeLayout bgLayout;
	private TextView buttonText;
	private ImageView imageBg;
	private String buttonString = "";
	private boolean superButton = false;
	private boolean focusBgChange = false;
	private boolean focusText = false;
	private boolean zoomButton = false;
	private boolean touchEnable = true;
	private boolean isAutoRecycleView = true;
	private boolean isRadioButton = false;
	private boolean isRadioButtonSelected = false;
	private RadioGroupManager radioGroupManager;
	private int normalBgColor = 0;
	private int focusBgColor = 0;
	private int radioBgColor = 0;
	private int normalTextColor = 0;
	private int focusTextColor = 0;
	private int radioTextColor = 0;
	private int normalResId = 0;
	private int focusResId = 0;
	private int radioResId = 0;
	private int currentButtonType;
	private OnClickListener clickListener;
	
	public SuperButton(Context context) {
		this(context, null);  
	}

	public SuperButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.unit_button, this);
		bgLayout = (RelativeLayout) layout.findViewById(R.id.buttonContenner);
		buttonText = (TextView) layout.findViewById(R.id.buttonContennerText);
		imageBg = (ImageView) layout.findViewById(R.id.imageButtonBg);
		
		readXMLAttribute(context, attrs);
	}
	
	public void setAutoRecycleView(boolean isAutoRecycleView) {
		this.isAutoRecycleView = isAutoRecycleView;
	}

	/**
	 * 读取自定义属性
	 * @param context
	 * @param attrs
	 */
	private void readXMLAttribute(Context context, AttributeSet attrs) {
		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SuperButton);
		setTextSize(array.getDimension(R.styleable.SuperButton_text_size, 14));
		currentButtonType = array.getInt(R.styleable.SuperButton_button_type, COLORBUTTON);
		buttonString = array.getString(R.styleable.SuperButton_button_text);
		normalTextColor = array.getInt(R.styleable.SuperButton_text_color, R.color.white);
		initDefaultButton(array, currentButtonType);
		array.recycle();  
	}

	private void initDefaultButton(TypedArray array, int type) {
		if (buttonString == null) buttonString = "";
		switch (type) {
		case TEXTBUTTON:
			int colorText = array.getInt(R.styleable.SuperButton_text_color, R.color.btn_orange_normal);
			int colorNormal = array.getInt(R.styleable.SuperButton_text_color_normal, R.color.btn_orange_normal);
			int colorFocus = array.getInt(R.styleable.SuperButton_text_color_focus, R.color.btn_orange_down);
			radioTextColor = array.getInt(R.styleable.SuperButton_text_color_radio, colorText);
			initTextButton(buttonString, colorText, colorNormal, colorFocus);
			break;
		case IMGZOOMBUTTON:
			radioResId = array.getResourceId(R.styleable.SuperButton_button_img_radio, R.drawable.ic_launcher);
			initZoomButton(array.getResourceId(R.styleable.SuperButton_button_img, R.drawable.ic_launcher));
			break;
		case COLORBUTTON:
			radioBgColor = array.getColor(R.styleable.SuperButton_button_color_radio, R.color.white);
			initButton(buttonString, array.getColor(R.styleable.SuperButton_text_color, R.color.white), array.getColor(R.styleable.SuperButton_button_color_normal,
					R.color.btn_green_normal), array.getColor(R.styleable.SuperButton_button_color_focus, R.color.btn_green_down));
			break;
		case IMGCHANGEBUTTON:
			radioResId = array.getResourceId(R.styleable.SuperButton_button_img_radio, R.drawable.ic_launcher);
			initSuperBbutton(array.getResourceId(R.styleable.SuperButton_button_img_normal, R.drawable.ic_launcher), 
							array.getResourceId(R.styleable.SuperButton_button_img_focus, R.drawable.ic_launcher), 
							array.getColor(R.styleable.SuperButton_button_color_bg, R.color.t_fake));
			break;
		}
	}
	
	/**
	 * 焦点切换背景图片按钮，用于使用2张不同背景图做按钮背景的情况
	 * @param normalResId	正常时的背景
	 * @param focusResId	获得焦点时的背景
	 */
	public void initSuperBbutton(int normalResId, int focusResId) {
    	initSuperBbutton(normalResId, focusResId, R.color.t_fake);
	}
	
	/**
	 * 焦点切换背景图片按钮，用于使用2张不同背景图做按钮背景的情况
	 * @param normalResId	正常时的背景
	 * @param focusResId	获得焦点时的背景
	 * @param bgColor		背景颜色
	 */
	public void initSuperBbutton(int normalResId, int focusResId, int bgColor) {
		superButton = true;
		focusBgChange = false;
		currentButtonType = IMGCHANGEBUTTON;
		this.normalResId = normalResId;
		this.focusResId = focusResId;
		imageBg.setImageBitmap(BitmapFactory.decodeResource(getResources(), normalResId));
		if (bgColor < 0) {
			setButtonBackgroundColor(bgColor);
        } else {
        	setButtonBackgroundColor(getResources().getColor(bgColor));
        }
	}
	
	//缩放的图片按钮
	public void initZoomButton(int resId) {
		initZoomButton(resId, buttonString);
	}
	public void initZoomButton(int resId, int bgColor) {
		initZoomButton(resId,buttonString);
		setButtonBackgroundColor(getResources().getColor(bgColor));
	}
	public void initZoomButton(int resId, String text) {
		zoomButton = true;
		focusBgChange = false;
		currentButtonType = IMGZOOMBUTTON;
		setButtonText(text);
		setTextColor(normalTextColor);
		this.normalResId = resId;
		imageBg.setImageBitmap(BitmapFactory.decodeResource(getResources(), resId));
		setButtonBackgroundColor(getResources().getColor(R.color.t_fake));
	}
	public void initZoomButton(Drawable drawable) {
		initZoomButton(drawable, buttonString);
	}
	public void initZoomButton(Drawable drawable, String text) {
		zoomButton = true;
		setButtonText(text);
		setTextColor(normalTextColor);
		imageBg.setImageDrawable(drawable);
		setButtonBackgroundColor(getResources().getColor(R.color.t_fake));
	}
	
	//带底色的文字按钮
	public void initButton(String buttonString) {
		initButton(buttonString, R.color.white);
	}
	
	public void initButton(String buttonString, int textColor) {
		initButton(buttonString, textColor, R.color.btn_green_normal, R.color.btn_green_down);
		focusBgChange(R.color.btn_green_normal, R.color.btn_green_down);
	}
	
    public void initButton(int textId, int textColor, int normalColor, int focusColor) {
        initButton(getResources().getString(textId), textColor, normalColor, focusColor);
    }
    
    public void initButton(String buttonString, int textColor, int normalColor, int focusColor) {
    	currentButtonType = COLORBUTTON;
        setButtonText(buttonString);
        if (textColor < 0) {
        	buttonText.setTextColor(textColor);
        } else {
        	buttonText.setTextColor(getResources().getColor(textColor));
        }
        if(normalColor < 0) {
        	setButtonBackgroundColor(normalColor);
        } else {
        	setButtonBackgroundColor(getResources().getColor(normalColor));
        }
        focusBgChange(normalColor, focusColor);
    }

  //纯文字按钮
  	public void initTextButton(int textId, int textColor, int normalColor, int focusColor) {
  		initTextButton(getResources().getString(textId), textColor, normalColor, focusColor);
  	}
  	
	public void initTextButton(String buttonString, int textColor, int normalColor, int focusColor) {
		currentButtonType = TEXTBUTTON;
		setButtonText(buttonString);
		if(textColor < 0) {
			buttonText.setTextColor(textColor);
		} else {
			buttonText.setTextColor(getResources().getColor(textColor));
		}
		setButtonBackgroundColor(getResources().getColor(R.color.t_fake));
		focusTextChange(normalColor, focusColor);
	}
	
	public void addRadioGroupManager(RadioGroupManager groupManager) {
		radioGroupManager = groupManager;
		radioGroupManager.addToRadioGroup(this);
		isRadioButton = true;
	}
	
	public void setTextSize(float size) {
		buttonText.setTextSize(size);
	}
	
	public void setTextColor(int colorId) {
		buttonText.setTextColor(colorId);
	}
	
	public void setButtonText(String text) {
		buttonText.setText(text);
	}
	
	public void setButtonTextDefultColor() {
		buttonText.setTextColor(getResources().getColor(R.color.white));	
	}
	
	public TextView getBottonText() {
		return buttonText;
	}
	
	public void setRadioResId(int resid) {
		radioResId = resid;
	}
	
	//是否开启按钮缩放功能
	public boolean isZoomButton() {
		return zoomButton;
	}

	public void setZoomButton(boolean zoomButton) {
		this.zoomButton = zoomButton;
	}
		
	public void setButtonBackgroundColor(int color) {
		bgLayout.setBackgroundColor(color);
	}
	
	public boolean isTouchEnable() {
		return touchEnable;
	}

	public void setTouchEnable(boolean touchEnable) {
		setEnabled(touchEnable);
		this.touchEnable = touchEnable;
	}

	public void recycleImageView() {
		if (imageBg.getDrawable() != null && imageBg.getDrawable() instanceof BitmapDrawable){
			Bitmap oldBitmap = ((BitmapDrawable)imageBg.getDrawable()).getBitmap();
			if (oldBitmap != null && !oldBitmap.isRecycled()) {
				oldBitmap.recycle();
				oldBitmap = null;
			}
		}
	}
	
	public void focusBgChange(int normalBgColor, int focusBgColor) {
		focusBgChange = true;
		focusText = false;
		this.normalBgColor = normalBgColor;
		this.focusBgColor = focusBgColor;
	}
	
	private void focusTextChange(int normalTextColor, int focusTextColor) {
		focusBgChange = false;
		focusText = true;
		this.normalTextColor = normalTextColor;
		this.focusTextColor = focusTextColor;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		if (!touchEnable) {
			return true;
		}
		if(event.getAction() == MotionEvent.ACTION_DOWN){ 
			if (superButton) {
				imageBg.setImageBitmap(BitmapFactory.decodeResource(getResources(), focusResId));
			}
			if (focusBgChange) {
				if (focusBgColor < 0) {
					setButtonBackgroundColor(focusBgColor);
				} else {
					setButtonBackgroundColor(getResources().getColor(focusBgColor));
				}
			}
			if (focusText) {
				if (focusTextColor < 0) {
					buttonText.setTextColor(focusTextColor);
				} else {
					buttonText.setTextColor(getResources().getColor(focusTextColor));
				}
			}
			if (zoomButton) {
				bgLayout.setScaleX(0.96f);
				bgLayout.setScaleY(0.96f);
			}
			
        }   
        else if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){  
        	if (superButton) {
				imageBg.setImageBitmap(BitmapFactory.decodeResource(getResources(), normalResId));
			}
        	if (focusBgChange) {
        		if (normalBgColor < 0) {
        			setButtonBackgroundColor(normalBgColor);
        		} else {
        			setButtonBackgroundColor(getResources().getColor(normalBgColor));
        		}
        	} 
        	if (focusText) {
        		if (normalTextColor < 0) {
					buttonText.setTextColor(normalTextColor);
				} else {
					buttonText.setTextColor(getResources().getColor(normalTextColor));
				}
        	}
        	if (zoomButton) {
				bgLayout.setScaleX(1.0f);
				bgLayout.setScaleY(1.0f);
			}
        	if (isRadioButtonSelected) {
        		onSelected();
        	}
        } 
//		return super.onTouchEvent(event);
		return true;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return true;
	}
	
	public void onClick(View v) {
		if (isRadioButton && radioGroupManager != null) {
			radioGroupManager.selectItem(this);
		}
		clickListener.onClick(v);
	}

	@Override
	public void setOnClickListener(OnClickListener l) {
		super.setOnClickListener(l);
		clickListener = l;
	}
	
	@Override
	protected void onDetachedFromWindow() {
		if (isAutoRecycleView) {
			imageBg.setImageBitmap(null);
		}
		super.onDetachedFromWindow();
	}

	@Override
	public void onSelected() {
		isRadioButtonSelected = true;
		if (currentButtonType == IMGZOOMBUTTON || currentButtonType == IMGCHANGEBUTTON) {
			imageBg.setImageBitmap(BitmapFactory.decodeResource(getResources(), radioResId));
			setButtonBackgroundColor(getResources().getColor(R.color.t_fake));
		}
		if (currentButtonType == TEXTBUTTON || currentButtonType == IMGZOOMBUTTON) {
			setTextColor(radioTextColor);
		}
		if (currentButtonType == IMGZOOMBUTTON) {
			setButtonBackgroundColor(radioBgColor);
		}
	}

	@Override
	public void onRelease() {
		isRadioButtonSelected = false;
		if (currentButtonType == IMGZOOMBUTTON || currentButtonType == IMGCHANGEBUTTON) {
			imageBg.setImageBitmap(BitmapFactory.decodeResource(getResources(), normalResId));
			setButtonBackgroundColor(getResources().getColor(R.color.t_fake));
		}
		if (currentButtonType == TEXTBUTTON || currentButtonType == IMGZOOMBUTTON) {
			setTextColor(normalTextColor);
		}
		if (currentButtonType == IMGZOOMBUTTON) {
			setButtonBackgroundColor(normalBgColor);
		}
	}
}
